package com.kaishengit.web.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.Member;
import com.kaishengit.util.Const;
import com.kaishengit.util.StringUtil;

public class Validate extends AbstractFilter {
	private String encoding = "UTF-8";
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		String temp = filterConfig.getInitParameter("encoding");
		if(StringUtil.isNotEmpty(temp)){
			encoding = temp;
			}
		}
	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		String uri = request.getRequestURI();
		uri = uri.substring(uri.lastIndexOf("/")+1);
	
		if("".equals(uri) || "index.jsp".equals(uri) || "index.jspx".equals(uri) || "login.jspx".equals(uri) ||uri.endsWith("png") || uri.endsWith("css") || uri.endsWith("js")){
			chain.doFilter(request, response);
		}else{
			HttpSession session = request.getSession();
			Member user = (Member) session.getAttribute(Const.USER);
			if(user == null){
				
				response.sendRedirect("index.jspx");
			}else{
				chain.doFilter(request, response);
			}
			
		}
	}

}
